package gazpachito.examples.services.cxf.vehicle.impl;

import java.util.ResourceBundle;

import gazpachito.examples.patterns.methodTemplate.api.AnimalVehicleDTO;
import gazpachito.examples.patterns.methodTemplate.api.EngineVehicleDTO;
import gazpachito.examples.patterns.methodTemplate.api.enums.VehicleEnum;
import gazpachito.examples.patterns.methodTemplate.business.exceptions.FacadeException;
import gazpachito.examples.patterns.methodTemplate.service.VehicleFacade;
import gazpachito.examples.services.cxf.vehicle.api.VehicleService;
import gazpachito.examples.services.cxf.vehicle.api.request.ManageVehicleRequest;
import gazpachito.examples.services.cxf.vehicle.api.response.ManageVehicleResponse;

import org.springframework.beans.factory.annotation.Autowired;

public class VehicleServiceImpl implements VehicleService {

	@Autowired
	private VehicleFacade vehicleFacadeImpl;

	@Override
	public String getVersion() {

		String version = ResourceBundle.getBundle("vehicle").getString(
				"vehicle.version");
		return version;
	}

	@Override
	public ManageVehicleResponse manageVehicle(ManageVehicleRequest request) {
		ManageVehicleResponse response = new ManageVehicleResponse();

		if (request != null && request.getVehicle() != null) {

			VehicleEnum template = null;

			if (request.getVehicle() instanceof AnimalVehicleDTO) {
				template = VehicleEnum.ANIMAL;
			} else if (request.getVehicle() instanceof EngineVehicleDTO) {
				template = VehicleEnum.ENGINE;
			}

			try {
				this.vehicleFacadeImpl.manageVehicle(request.getVehicle(),
						template);
			} catch (FacadeException e) {

				response.setErrorMessage(e.getMessage());
			}
		}
		return response;
	}
}
